package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Employee;
import com.itheima.service.Employeeservice;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class Employeecontorller {

    @Autowired
    private Employeeservice employeeservice;

    @Autowired
    private HttpServletRequest request;

    // 登录功能
    @PostMapping("/login")
    public R loginpage(@RequestBody Employee employee) {

        //1.从页面获取到密码并使用MD5 加密保存
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页面提供的username在书库进行查询
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeservice.getOne(lqw);

        //3、如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("用户名不存在!");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("用户名或者密码错误!");
        }

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账户已被禁用!");
        }

        //6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("empl", emp.getId());
        return R.success(emp);
    }

    //退出功能
    @PostMapping("/logout")
    public R loginout() {
        request.getSession().removeAttribute("empl");
        return R.success("退出成功!");
    }

    /**
     * 添加功能
     *
     * @param emp
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee emp) {
        log.info("新增员工,员工信息为: {}", emp.toString());

        //设置初始化的密码为:123456,并用MD5方法进行加密处理
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
/*

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        //获取当前登录用户的id
        long emplid = (long) request.getSession().getAttribute("empl");

        emp.setCreateUser(emplid);
        emp.setUpdateUser(emplid);
*/

        employeeservice.save(emp);
        return R.success("保存成功!");
    }
    //分页查询,根据名字(模糊查询)
    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {

        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime);
        employeeservice.page(pageInfo, lqw);
        return R.success(pageInfo);
    }

    @PutMapping
    public R update(@RequestBody Employee emp) {

        /*long emplId = (long) request.getSession().getAttribute("empl");

        emp.setUpdateTime(LocalDateTime.now());
        emp.setUpdateUser(emplId);*/

        employeeservice.updateById(emp);
        return R.success("员工信息修改成功!");
    }

    @GetMapping("/{id}")
    public R updatebyid(@PathVariable long id) {

        Employee byId = employeeservice.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("没有查询到对应的员工信息!");
    }

    /*@PutMapping
    public R updateAll(@RequestBody Employee emp) {

        long empl = (long) request.getSession().getAttribute("empl");

        emp.setUpdateTime(LocalDateTime.now());
        emp.setUpdateUser(empl);

        employeeservice.updateById(emp);
        return R.success("员工信息修改成功!");
    }*/

}
